package org.small.shop.phone.api.process.goods.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.small.shop.phone.api.process.goods.mapper.TblWxGoodsMapper;
import org.small.shop.phone.api.process.goods.pojo.GoodsInfo;
import org.small.shop.phone.api.process.goods.pojo.TblWxGoods;
import org.small.shop.phone.api.process.goods.pojo.WxShowGoodsVo;
import org.small.shop.phone.api.process.goodsappraises.mapper.TblWxGoodsAppraisesMapper;
import org.small.shop.phone.api.process.goodsappraises.pojo.TblWxGoodsAppraisesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TblWxGoodsServiceImpl implements TblWxGoodsService {

	@Autowired
	private TblWxGoodsMapper goodsMapper;

	@Autowired
	private TblWxGoodsAppraisesMapper tblWxGoodsAppraisesMapper;

	@Override
	public List<WxShowGoodsVo> getGoodsListByType(WxShowGoodsVo goodsVo) {

		Integer currentPage = goodsVo.getCurrentPage();
		Integer perPage = goodsVo.getPerPage();
		if (currentPage != null && currentPage != -1 && perPage != null) {
			goodsVo.setStartNum((currentPage - 1) * perPage);
		} else {
			goodsVo.setStartNum(0);
			goodsVo.setPerPage(10);
		}
		Integer cuount = goodsMapper.selectGoodsByTypeCount(goodsVo);
		goodsVo.setTotalNum(cuount);

		return goodsMapper.selectGoodsListByType(goodsVo);
	}

	@Override
	public List<WxShowGoodsVo> getGoodsListPopularSelling() {

		return goodsMapper.getGoodsListPopularSelling();
	}

	@Override
	public TblWxGoods selectGoodsByGoodsId(Integer goodsId) {

		List<TblWxGoodsAppraisesVo> appraiseList = tblWxGoodsAppraisesMapper.selectTwoApprByGoodsid(goodsId);
		Integer count = tblWxGoodsAppraisesMapper.selectAllApprByGoodsidCount(goodsId);
		TblWxGoods goods = goodsMapper.selectGoodsByGoodsId(goodsId);
		goods.setAppraiseList(appraiseList);
		goods.setTotalApprs(count);
		return goods;
	}

	@Override
	public List<GoodsInfo> getGoodsInfoList(String[] goodsIds, String[] skuIds) {

		List<GoodsInfo> goodsInfoList = new ArrayList<GoodsInfo>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < goodsIds.length; i++) {
			String goodsId = goodsIds[i];
			String skuId = skuIds[i];
			map.put("goodsId", goodsId);
			map.put("skuId", skuId);
			GoodsInfo goodsInfo = goodsMapper.getGoodsInfoByGoodsIdAndSkuId(map);
			if (goodsInfo != null) {
				goodsInfoList.add(goodsInfo);
			}
		}
		return goodsInfoList;
	}

}
